package com.yuanqiworld.vw.domain;

import flexjson.JSONSerializer;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

@RooJavaBean
@RooToString
@RooJson
@RooJpaActiveRecord(table = "vw_scene", finders = "findScenesBySceneName")
public class Scene {
    @NotNull
    @Column(unique = true)
    private String sceneName;

    private String sceneType;

    private String background;

    private boolean showCharacter;

    @Embedded
    private Transform transform;

    @NotNull
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinTable(name = "vw_scene_camera",
            joinColumns = {@JoinColumn(name = "scene_id")},
            inverseJoinColumns = {@JoinColumn(name = "camera_id")})
    private Device camera;

    @ManyToMany
    @JoinTable(name = "vw_scene_device",
            joinColumns = {@JoinColumn(name = "scene_id")},
            inverseJoinColumns = {@JoinColumn(name = "device_id")})
    private Set<Device> devices = new HashSet<Device>();

    public String toJson() {
        return new JSONSerializer().exclude("*.class").include("*.devices").serialize(this);
    }
}
